home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / fileutil.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  4.0 KB  |  135 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Functions to handle moving/deleting files, especially on windows where file
  5. locking semantics can cause problems.
  6. '''
  7. import logging
  8. import os
  9. import shutil
  10. import eventloop
  11.  
  12. def migrate_file(source, dest, callback, retry_after = 10, retry_for = 60):
  13.     '''Try to migrate a file, if this works, callback is called.  If we fail
  14.     because the file is open, we retry migrating the file every so often (by
  15.     default every 10 seconds, stopping after 60 seconds).  This probably only
  16.     makes a difference on Windows.
  17.     '''
  18.     
  19.     try:
  20.         shutil.move(source, dest)
  21.     except EnvironmentError:
  22.         e = None
  23.         logging.warn('Error migrating %s to %s (Error: %s)', source, dest, e)
  24.         
  25.         try:
  26.             os.remove(dest)
  27.         except EnvironmentError:
  28.             pass
  29.  
  30.         if retry_for > 0:
  31.             if e.errno == 13:
  32.                 logging.info('Retrying migration')
  33.                 eventloop.addTimeout(retry_after, migrate_file, 'Migrate File Retry', args = (source, dest, callback, retry_after, retry_for - retry_after))
  34.             
  35.         
  36.     except TypeError:
  37.         e = None
  38.         logging.warn('Type error migrating %s (%s) to %s (%s) (Error %s)', source, type(source), dest, type(dest), e)
  39.         raise 
  40.  
  41.     callback()
  42.  
  43.  
  44. class DeletesInProgressTracker(set):
  45.     
  46.     def __init__(self):
  47.         self.set = set()
  48.  
  49.     
  50.     def normalize(self, path):
  51.         return os.path.abspath(os.path.normcase(path))
  52.  
  53.     
  54.     def add(self, path):
  55.         self.set.add(self.normalize(path))
  56.  
  57.     
  58.     def discard(self, path):
  59.         self.set.discard(self.normalize(path))
  60.  
  61.     
  62.     def __contains__(self, path):
  63.         return self.normalize(path) in self.set
  64.  
  65.  
  66. deletes_in_progress = DeletesInProgressTracker()
  67.  
  68. def delete(path, retry_after = 10, retry_for = 60):
  69.     '''Try to delete a file or directory.  If this fails because the file is
  70.     open, we retry deleting the file every so often This probably only makes a
  71.     difference on Windows.
  72.     '''
  73.     
  74.     try:
  75.         if os.path.isfile(path):
  76.             os.remove(path)
  77.         elif os.path.isdir(path):
  78.             shutil.rmtree(path)
  79.     except EnvironmentError:
  80.         e = None
  81.         logging.warn('Error deleting %s', path)
  82.         if retry_for > 0 and e.errno == 13:
  83.             deletes_in_progress.add(path)
  84.             logging.info('Retrying delete')
  85.             eventloop.addTimeout(retry_after, delete, 'Delete File Retry', args = (path, retry_after, retry_for - retry_after))
  86.         
  87.     except:
  88.         e.errno == 13
  89.  
  90.     deletes_in_progress.discard(path)
  91.  
  92.  
  93. def miro_listdir(directory):
  94.     """Directory listing that's safe and convenient for finding new videos in
  95.     a directory.
  96.  
  97.     Returns the tuple (files, directories) where both elements are a list of
  98.     absolute pathnames.  OSErrors are silently ignored.  Hidden files aren't
  99.     returned.  Pathnames are run through os.path.normcase.
  100.     """
  101.     files = []
  102.     directories = []
  103.     directory = os.path.abspath(os.path.normcase(directory))
  104.     if directory in deletes_in_progress:
  105.         return None
  106.     
  107.     
  108.     try:
  109.         listing = os.listdir(directory)
  110.     except OSError:
  111.         return ([], [])
  112.  
  113.     for name in listing:
  114.         if name[0] == '.' or name.lower() == 'thumbs.db':
  115.             continue
  116.         
  117.         path = os.path.join(directory, os.path.normcase(name))
  118.         if path in deletes_in_progress:
  119.             continue
  120.         
  121.         
  122.         try:
  123.             if os.path.isdir(path):
  124.                 directories.append(path)
  125.             else:
  126.                 files.append(path)
  127.         continue
  128.         except OSError:
  129.             continue
  130.         
  131.  
  132.     
  133.     return (files, directories)
  134.  
  135.